//
//  KanJinNangViewController.m
//  TravelYo
//
//  Created by lanou3g on 15/10/29.
//  Copyright © 2015年 TravelYo. All rights reserved.
//

#import "KanJinNangViewController.h"

@interface KanJinNangViewController () <UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIAlertViewDelegate>

@property (nonatomic, strong) NSArray *tableArray;


@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) NSArray *collectionArray;

@end

@implementation KanJinNangViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    [self addSubViews];
    
    [self setUpDataWithUrl:kJinNangZuiXinAPI]; // 解析数据
    // Do any additional setup after loading the view.
}

// 添加子视图
- (void)addSubViews
{
    self.tableArray = @[@"最新", @"亚洲", @"欧洲", @"北美洲", @"南美洲", @"大洋洲", @"非洲", @"专题锦囊", @"中国内地"];
    self.collectionArray = @[kJinNangZuiXinAPI, kJinNangyazhouAPI, kJinNangouzhouAPI, kJinNangbeimeizhouAPI, kJinNangnanmeizhouAPI, kJinNangdayangzhouAPI, kJinNangfeizhouAPI, kJinNangzhuantijinnangAPI, kJinNangzhongguoneidiAPI];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 4, kScreenHeight) style:(UITableViewStylePlain)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // self.tableView.bounces = NO;
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    self.tableView.showsVerticalScrollIndicator = NO; // 隐藏滑动条
    [self.view addSubview:self.tableView];
    
    
    // 网络状布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 5; // 行间距
    layout.minimumLineSpacing = 5; // 列间距
    layout.itemSize = CGSizeMake(((kScreenWidth * 3 / 4) - 30 ) / 2, (kScreenHeight - 100) / 3); // item的size大小
    layout.scrollDirection = UICollectionViewScrollDirectionVertical; // 滑动方向
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(0, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(kScreenWidth / 4, 0, kScreenWidth * 3 / 4, kScreenHeight - 64) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    // 设置代理
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.collectionView.bounces = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
    
 
    // 注册
    [self.collectionView registerClass:[TypeKanJinNangViewCell class] forCellWithReuseIdentifier:@"jinNangCell"];
    
    [self.collectionView registerClass:[TypeJinNangHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"jinNangHeaderView"];
    
}

#pragma mark --- tableView 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenHeight / 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.tableArray[indexPath.row];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    
    UIView *selecedView = [[UIView alloc] init];
    selecedView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = selecedView;
    cell.highlighted = YES;
    cell.textLabel.highlightedTextColor = kMainColor;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.dataArray = nil;
    NSString *str = self.collectionArray[indexPath.row];
    [self setUpDataWithUrl:str];
    
}


#pragma mark --- collectionView 代理方法
// items数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    KanjinNangModel *model = self.dataArray[section];
    return model.dataArray.count;
}

// 分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataArray.count;
}

// items
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TypeKanJinNangViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"jinNangCell" forIndexPath:indexPath];
    // cell.contentView.backgroundColor = [UIColor redColor];
    if (self.dataArray != 0) {
        KanjinNangModel *model = self.dataArray[indexPath.section];
        CountryModel *countryModel = model.dataArray[indexPath.row];
        NSString *url = [NSString stringWithFormat:@"%@/140_210.jpg?cover_updatetime=%@", countryModel.cover, countryModel.cover_updatetime];
        [cell.imageV sd_setImageWithURL:[NSURL URLWithString:url]];
    }
    return cell;
}

// 分区表头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        TypeJinNangHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"jinNangHeaderView" forIndexPath:indexPath];
        KanjinNangModel *model = self.dataArray[indexPath.section];
        headerView.titleLabel.text = model.name;
        return headerView;
    }
    return nil;
}

// 点击collectionCell的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"第%ld分区, 第%ld行", indexPath.section, indexPath.row);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"本模块暂未开启" preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:nil];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [alert dismissViewControllerAnimated:YES completion:nil];
    });
}


- (void)setUpDataWithUrl:(NSString *)url
{
    [[TYDataManager shareManager] requestDataWithUrl:url block:^(NSData *data){
        NSMutableDictionary *diction = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = diction[@"data"];
        for (NSDictionary *dic in array) {
            KanjinNangModel *model  = [[KanjinNangModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            
            NSArray *arr = dic[@"guides"];
            NSMutableArray *dataArray = [NSMutableArray array];
            for (NSDictionary *dict in arr) {
                CountryModel *countryModel = [[CountryModel alloc] init];
                [countryModel setValuesForKeysWithDictionary:dict];
                [dataArray addObject:countryModel];
            }
            model.dataArray = dataArray;
            [self.dataArray addObject:model];
        }
        [self.collectionView reloadData];
        [self.collectionView setContentOffset:CGPointMake(0, 0) animated:YES];
    }];
    
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
